package com.example.shrio.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.util.APIState;
import com.example.util.AjaxResult;
import com.example.util.CurrentUser;
import com.example.util.RedisKeys;

@RestController
@RequestMapping(value = "/shrio")
public class ShrioController {

	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;

	/**
	 * 凭证验证统一返回数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/unauth")
	public Object unauth(HttpServletRequest request) {
		String token = request.getHeader("token");
		if (token != null) {
			return new AjaxResult(APIState.S98, "凭证失效");
		}
		return new AjaxResult(APIState.S97, "未登录");
	}

	/**
	 * 拦截统一登出
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/logout")
	public Object logout(HttpServletRequest request) {
		CurrentUser.logout();
		redisTemplate.delete(RedisKeys.getShiroSessionKey(request.getSession().getId()));
		return new AjaxResult(APIState.S97, "当前账户在其他地方登陆");
	}

}
